package com.test.aninterface;

import com.test.aninterface.IPrint;

import org.checkerframework.checker.units.qual.C;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ServiceLoader;

public class spiFactory<T> {
    private ArrayList<T> iPrints = new ArrayList<>();

    public spiFactory(Class<T> cls){
        ServiceLoader<T> serviceLoader = ServiceLoader.load(cls);
        for(T t : serviceLoader){
            if(t != null){
                iPrints.add(t);
            }
        }
    }

    public ArrayList<T> getIPrints(){
       return  iPrints;
    }

    public T getObject(String name){
        for(T t1 : iPrints){
            if(t1.getClass().getName().contains(name)){
                return t1;
            }
        }
        return null;
    }

    public  ArrayList<String> getAllClassName(){
        ArrayList<String> arrayList = new ArrayList<>();

        for(T t : iPrints){
            arrayList.add(t.getClass().getName());
        }
        return arrayList;

    }

}
